
const Controller = require('./base_controller')

class AccountSerFeaController extends Controller {
  constructor (...props) {
    super(...props)
    this.rule = {
      accountId: 'int', 
      serviceId: 'int',
      featureId: 'int',
      extend: 'json',
      featuresPattern: 'string?'
    }
  }
  //查询
  async index (ctx) {
    let { userId } = ctx.params
    try {
      let serveList = await ctx.service.accountSerFeaService.index(userId)
      this.success({data: serveList.rows})
    } catch (e) {
      this.error({
        msg: '查询失败!',
        data: e
      })
    }
  }
  //创建
  async create (ctx) {
    let { body } = ctx.request
    try {
      ctx.validate(this.rule)
      await ctx.service.accountSerFeaService.create(body)
      this.success({
        msg: '创建成功!',
        data: body
      })
    } catch (e) {
      let msg = (e && e.invalid_param) 
        ? '参数错误!' 
        : '创建失败,请稍后再试!'
      this.error({
        msg: msg,
        data: e
      })
    }
  }
  //修改
  async update (ctx) {
    let { params, request } = ctx
    try {
      ctx.validate(this.rule)
      let serve = Object.assign(params, request.body)
      await ctx.service.accountSerFeaService.update(serve)
      this.success({
        msg: '修改成功!',
        data: serve
      })
    } catch (e) {
      let msg = (e && e.invalid_param)
        ? '参数错误!' 
        : '修改失败，请稍后再试!'
      this.error({
        msg: msg,
        data: e
      })
    }
  }
  //删除
  async delete (ctx) {
    let { params: { id } } = ctx
    try {
      await ctx.service.accountSerFeaService.delete(id)
      this.success({
        data: { id }
      })
    } catch (e) {
      this.error({
        msg: '删除失败!',
        data: e
      })
    }
  }
}

module.exports = AccountSerFeaController
